﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;


namespace HSchSchedule.AjaxPages
{
    public partial class AjaxFixSubjectLession : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetGroup()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Group> lstGroup = DataBase.GetAllGroup(schId);
                    List<JDGroup> lstJDGroup = new List<JDGroup>();
                    foreach (Group g in lstGroup)
                    {
                        lstJDGroup.Add(AssignHelp.GroupToJDGroup(g));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJDGroup);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetDay()
        {
            int cusId = 0;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Day> lstDay = DataBase.GetAllDay(schId);
                    List<JDay> lstJDay = new List<JDay>();
                    foreach (Day d in lstDay)
                    {
                        if (d.DayName.CompareTo("All") != 0)
                        {
                            lstJDay.Add(AssignHelp.DayToJDay(d));
                        }
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJDay);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetLession()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Lession> lstLession = DataBase.GetAllLession(schId);
                    List<JLession> lstJLession = new List<JLession>();
                    foreach (Lession l in lstLession)
                    {
                        lstJLession.Add(AssignHelp.LessionToJLession(l));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJLession);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string GetFreeSubject(int id)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Subject> lstSub = DataBase.GetSubjectOfGroup(id);

                    List<JSubject> lstJSub = new List<JSubject>();
                    foreach (Subject s in lstSub)
                    {
                        if (s.NotNeedTeacher > 0)
                        {
                            lstJSub.Add(AssignHelp.SubToJSub(s));
                        }
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSub);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string GetFSL(int groupId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<FixSubjectSessionLession> lstFSL = DataBase.GetFixSubjectGroup(groupId);
                    List<JFixSubjectSessionLession> lstJFSL = new List<JFixSubjectSessionLession>();
                    foreach (FixSubjectSessionLession fsl in lstFSL)
                    {
                        lstJFSL.Add(AssignHelp.FixSSLToJFixSSL(fsl));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJFSL);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
        [WebMethod(true)]
        public static string AddFSL(List<JFixSubjectSessionLession> lstJFSL)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    bool ok = true;
                    List<FixSubjectSessionLession> lstFSL = new List<FixSubjectSessionLession>();
                    foreach (JFixSubjectSessionLession jfsl in lstJFSL)
                    {
                        ok = ValidateHelp.ValidateFSL(jfsl);

                        if (!ok)
                        {
                            break;
                        }
                        else
                        {
                            FixSubjectSessionLession fsl = AssignHelp.JFixSSLToFixSSL(jfsl);
                            fsl.ScheduleInListId = schId;
                            lstFSL.Add(fsl);
                        }
                    }
                    if (ok)
                    {
                        DataBase.SaveFSL(lstFSL);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
    }
}
